package cn.aitrox.ry.bean;

import cn.aitrox.ry.enumtype.RespCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespResult<T> implements Serializable {

    //响应数据结果集
    private T data;

    /**
     * 状态码
     * 20000 操作成功
     * 50000 操作失败
     */
    private Integer code;

    /***S
     * 响应信息
     */
    private String msg;

    public RespResult(RespCodeEnum resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
    }

    public RespResult(T data, RespCodeEnum resultCode) {
        this.data = data;
        this.code = resultCode.getCode();
        this.msg = resultCode.getMessage();
    }

    public static RespResult ok() {
        return new RespResult(null, RespCodeEnum.SUCCESS);
    }

    public static RespResult ok(Object data) {
        return new RespResult(data, RespCodeEnum.SUCCESS);
    }

    public static RespResult error() {
        return new RespResult(null, RespCodeEnum.ERROR);
    }

    public static RespResult error(String message) {
        return secByError(RespCodeEnum.ERROR.getCode(), message);
    }

    public static RespResult error(RespCodeEnum resultCode) {
        return new RespResult(resultCode);
    }

    //自定义异常
    public static RespResult secByError(Integer code, String msg) {
        RespResult err = new RespResult();
        err.setCode(code);
        err.setMsg(msg);
        return err;
    }

    public Boolean _isOk() {
        return RespCodeEnum.SUCCESS.getCode().equals(this.code);
    }
}
